#pragma once

#pragma warning(disable:4201)
#include "SvrBase.h"
#include "../../Lib_Base/Inc\mempool.h"
#include "Types.h"

namespace sbase
{
	//////////////////////////////////////////////////////////////////////
	class IMessage
	{
	public:
		IMessage(void);
		IMessage(const void* buf, int from=-1, int to=-1)
		{	
			usFrom=from;
			usTo=to;
			keepHead = 1;
			ZeroMemory(bufMsg, sizeof(bufMsg));
			if (buf)
			{
				USHORT size = *((USHORT*) buf);
				IF_OK (size <= _MAX_MSGSIZE)
					memcpy_s(bufMsg,sizeof(bufMsg), buf, size);
			}

		}

		IMessage(const void* buf, bool bkeepHead, int from=-1, int to=-1)
		{	
			usFrom=from;
			usTo=to;
			keepHead = bkeepHead;
			ZeroMemory(bufMsg, sizeof(bufMsg));
			if (buf)
			{
				USHORT size = *((USHORT*) buf);
				IF_OK (size <= _MAX_MSGSIZE)
					memcpy_s(bufMsg,sizeof(bufMsg), buf, size);
			}
		}

		IMessage(int size, int type, int msgIndex, const void* buf, int from, int to, bool bkeepHead = true);
		IMessage(const IMessage& rhs); 

		int			Release	(void)	;

		int			GetSize	(void) const	{ return usMsgSize; }
		int			GetType	(void) const	{ return usMsgType; }
		const void*		GetBuf	(void) const	{ return bufMsg; }

		int			GetFrom	(void) const	{ return usFrom; }
		int			GetTo	(void) const	{ return usTo; }
		void		SetKeepHead( bool bKeep) { keepHead = bKeep;}
		bool		GetKeepHead()			{ return keepHead;}		

// 		void*	operator new	(size_t size);
// 		void	operator delete	(void* p);
// 
// 		void*	operator new[]	(size_t size);
// 		void	operator delete[](void* p);

		int		usFrom, usTo;
		bool	keepHead;
		union 
		{
			char	bufMsg[_MAX_MSGSIZE];
			struct 
			{
				uint16 usMsgSize;
				uint16 usMsgType;
				uint16 usMsgIndex;
			};
		};
	public:
		DECLARE_FIXEDSIZE_ALLOCATOR(IMessage);
	};

}
